package hy.controller;

import hy.beans.Book;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
public class SolrCtrl {
    @Autowired
    HttpSolrClient sc;

    @RequestMapping("/addBook")
    public void add()throws Exception{
        Book b1=new Book(1,"孙子兵法","兵者，诡道也，国之利器，生死之地，存亡之道，不可不查也","孙武",12);
        Book b2=new Book(2,"三国演义","天下事，分久不合，合久必分","罗贯中",10);
        Book b3=new Book(3,"西游记","一只猴子也一群神仙妖怪之间的恩怨故事!","吴承恩",18);
        List<Book>list=new ArrayList<>();
        list.add(b1);
        list.add(b2);
        list.add(b3);
        for(Book b:list){
            SolrInputDocument sid=new SolrInputDocument();
            sid.addField("id",b.getId());
            sid.addField("bookname",b.getName());
            sid.addField("author",b.getAuthor());
            sid.addField("bookintroduce",b.getIntroduce());
            sid.addField("bookprice",b.getPrice());
            sc.add(sid);
            sc.commit();
        }
    }
    @RequestMapping("/searchBook")
    public Map<String,Object> search(String kw, Integer pageNo, Integer pageSize) throws IOException, SolrServerException {
        String param=null;
        if(kw==null||kw.equals("")){
            param="*:*";
        }else{
            param="bookname:"+kw+" OR bookintroduce:"+kw;
        }

        SolrQuery q=new SolrQuery();
        q.setQuery(param);
        //指定按哪个字段排序
        q.setSort("bookprice", SolrQuery.ORDER.asc);

        //设置高亮显示的样式
        q.setHighlight(true);
        q.setHighlightSimplePre("<font colr='red'");
        q.setHighlightSimplePost("</font>");

        q.setStart(pageSize*(pageNo-1));
        q.setRows(pageSize);
        QueryResponse resp=sc.query(q);

        //开始解析查询的结果
        SolrDocumentList rs=resp.getResults();
        Long total=rs.getNumFound();
        System.out.println("一共查到"+total+",当前页"+rs.size());
        List<Map<String,String>> list=new ArrayList<>();
        for(SolrDocument sd:rs){
            Map<String,String>vo=new HashMap<>();
            vo.put("id",(String)sd.getFirstValue("id"));
            vo.put("bookname", (String) sd.getFirstValue("bookname"));
            vo.put("author", (String) sd.getFirstValue("author"));
            vo.put("bookintroduce", (String) sd.getFirstValue("bookintroduce"));
            vo.put("bookprice",sd.getFirstValue("bookprice")+"");
            list.add(vo);
        }
        Map<String,Object> m=new HashMap<>();
        m.put("list",list);
        m.put("total",total);
        m.put("pagesTotal",(total%pageSize!=0)?(total/pageSize+1):total/pageSize);
        m.put("pageNum",pageNo);
        m.put("pageSize",pageSize);
        return m;
    }
}